<template>
  <div class="InfoBox roundy-20 shadow">
    <div class="title">
      <div class="userinfo fontSize-20" style="color: rgb(0, 0, 167); font-weight: bold;">
        <i>{{ user.userName }}</i>
        <div class="imgBar pointer">
          <img class="uploadimg" :src="blobToString(user?.userImg)" alt="上传的图片" style="margin-right: 30%;" />
          <div class="imgTip cantSelect" @click="showModal">点击修改</div>
        </div>
        <!-- <img style="cursor: pointer;" :src=this.user.userImg @click="showModal"> -->

        <table>
          <tr class="items">
            <td class="itemstitle">注册邮箱:</td>
            <td class="itemsinfo">{{ user.userEmail }}</td>
          </tr>
          <tr class="items">
            <td class="itemstitle">注册时间:</td>
            <td class="itemsinfo">{{ user.registrationDate }}</td>
          </tr>
          <tr class="items">
            <td class="itemstitle">用户状态:</td>
            <td class="itemsinfo">{{ user.userState==0? '在线':'离线' }}</td>
          </tr>
          <tr class="items">
            <td class="itemstitle">用户类型:</td>
            <td class="itemsinfo">{{ user.userType == 0 ? '普通用户' : '管理员' }}</td>
          </tr>
        </table>

        <login-button @click="showModal" class="rightFloat" style="margin-left: 35%; margin-top: 30px" title="修改个人信息" />
        <!-- 修改个人信息 -->
        <div v-if="isModalVisible" class="modal">
          <div class="modal-content">
            <h2>修改个人信息</h2>
            <!-- 修改头像 -->
            <div style="cursor: pointer;">
              <div class="imgBar">
                <img class="uploadimg" :src="blobToString(user?.userImg)" alt="上传的图片" style="margin-right: 30%;" />
                <div class="imgTip" @click="openFileInput">点击更换</div>
              </div>
              <input ref="fileInput" type="file" style="display: none" @change="handleFileUpload" accept="image/*" />
            </div>

            <!-- 修改用户名 -->
            <div>
              <LoginInput class="input" title="请输入要修改的用户名" type="text" v-model="reviseName" />
            </div>

            <div style="margin-top: 50px;display: flex; justify-content: space-around">
              <button @click="reviseData()" style="background-color: rgb(56, 148, 252);color: white;">确认</button>
              <button @click="hideModal" style="background-color: aliceblue;">关闭</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import LoginButton from './LoginButton.vue';
import LoginInput from './LoginInput.vue';
import request from '@/utils/request';

export default {
  name: 'UserInfo',
  props: ['user'],
  components: {
    LoginButton,
    LoginInput
  },
  data() {
    return {
      reviseName: this.user.userName,
      //选择的图片文件
      selectedFile: null,
      //窗口显示
      isModalVisible: false,
      //图片可上传的最大尺寸（单位kb）
      maxSize: 100,
    }
  },
  methods: {
    //转换图片格式
    blobToString(img) {
      if (img && typeof img === 'string') {
        var slice = img.slice(0, 11);
      }

      //判断图片格式是否正确
      if (slice == "data:image/") {

        return img;
      }
      var data = "data:image/png;base64," + img;
      return data;
    },
    // 弹出窗口
    showModal() {
      this.isModalVisible = true;
      this.reviseName = this.user.userName
    },
    //隐藏窗口
    hideModal() {
      this.isModalVisible = false;
    },
    //打开文件上传
    openFileInput() {
      this.$refs.fileInput.click();
    },
    //修改图片
    handleFileUpload(event) {
      const file = event.target.files[0];
      const reader = new FileReader();
      if(!file)
        return
      if(file?.size > this.maxSize *1024){
        this.$message({
          message: '图片过大！',
          type: 'error'
        })
        return
      }

      reader.onload = (e) => {
        this.$props.user.userImg = e.target.result;
        console.log(e.target.result)
        let userImg = e.target.result

        request.post('/api/start/edit/userImg', {userid:this.user?.userid, userImg}).then(
          response  => { console.log(response.data); },
          error => {console.log(error.message);}
        )

      };

      reader.readAsDataURL(file);
    },
    //修改上交
    reviseData() {
      this.isModalVisible = false;
      this.$props.user.userName = this.reviseName
      request.post("/api/start/edit/userName", {userName:this.reviseName, userid: this.user?.userid}).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
  },
}
</script>

<style scoped>
.left {
  height: 100vh;
  width: 50%;
  text-align: center;
  line-height: 100vh;
}

.right {
  height: 100vh;
  width: 50%;
  text-align: center;
  line-height: 100vh;
}

.InfoBox {
  height: 600px;
  width: 500px;
  margin: 0px auto;
  /* border: 1px solid rgb(204, 204, 204); */
  background-color: white;
}

.shadow {
  box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.5);
}

.title {
  text-align: center;
  padding: 50px;
}

.text-1 {
  font-size: 20px;
}

.toRegisterBtn {
  color: rgb(94, 94, 194);
  font-size: 18px;
  margin-left: 60px;
}

img {
  width: 150px;
  height: 150px;
  border-radius: 75px;
  margin-left: 30%;
}

.imgBar {
  width: 150px;
  height: 150px;
  border-radius: 75px;
  margin-left: 30%;
  overflow: hidden;
}

.imgBar img {
  width: 150px;
  height: 150px;
  border-radius: 0px;
  margin-left: 0;
}

.imgBar:hover .imgTip {
  transition: all 0.2s;
  transform: translate(0, -155px);
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  height: 150px;
  line-height: 150px;
  opacity: 1;
}

.imgTip {
  opacity: 0;
  transition: all 0.2s;
  transform: translate(0, -155px);
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  height: 150px;
  line-height: 150px;
}

.userinfo {
  display: flex;
  flex-direction: column;
  text-align: center;

}

.items {
  display: flex;
  flex-direction: row;
  text-align: left;
}

.itemsinfo {
  margin-left: 30%;
  color: grey;
  font-weight: lighter;
  font-size: 16px;
}

tr {
  margin-top: 30px;
}

.tr td {
  border-bottom: 1px solid;
  margin-top: 10px;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-content {
  width: 350px;
  height: 500px;
  background-color: white;
  padding: 20px;
  border-radius: 4px;
}

.modal-content button {
  width: 120px;
  height: 35px;
  background-color: white;
  border: 1px grey solid;
  border-radius: 10px;
}

input {
  border: none;
  outline: none;
  height: 100%;
  font-size: 16px;
  z-index: 2;
  background-color: transparent;
  width: 90%;
}

.inputBox {
  transition: all 0.2s;
  height: 60px;
  width: 100%;
  border: 1px solid rgb(207, 207, 207);
  padding: 5px;
  padding-left: 15px;
  box-sizing: border-box;
  line-height: 42px;
  text-align: left;
  margin-top: 20px;
}
</style>
